#include "rtsp_service.h"
#include "rtsp_ringBuffer.h"

RtspService::RtspService(): _task_scheduler(NULL),
						    _usage_env(NULL),
							_authDB(NULL),
							_rtspServer(NULL),
							_serverMediaSession(NULL),
							_videoSource(NULL),
							_videoliveSMS(NULL),
							_rtsp_ringBuffer(NULL),
							_isInit(false),
							_isStart(false)
{

	OutPacketBuffer::maxSize = 600000;

}

RtspService::~RtspService()
{

}

int32_t RtspService::init(){

	if( _isInit ){
		printf( "RtspService already Init\r\n" );
		return -1;
	}

    _task_scheduler = BasicTaskScheduler::createNew();
    if(_task_scheduler == NULL){
        return -1;
    }
	_usage_env = BasicUsageEnvironment::createNew(*_task_scheduler);
    if(_usage_env == NULL){
        printf( "create _usage_env failed \r\n" );
        return -1;
    }

	// if( username != NULL && passwd != NULL ){

	// 	_authDB = new UserAuthenticationDatabase;
	// 	if(_authDB == NULL){
	// 		*_usage_env << "Failed to create _authDB: " << _usage_env->getResultMsg() << "\n";
	// 		return -1;
    // 	}
	// 	_authDB->addUserRecord(username, passwd); // replace these with real strings

	// }

	_rtspServer = RTSPServer::createNew(*_usage_env, 554, _authDB);
	if (_rtspServer == NULL) {
		*_usage_env << "Failed to create RTSP server: " << _usage_env->getResultMsg() << "\n";
		exit(1);
	}

	_isInit = true;

    return 0;
}

int32_t RtspService::deinit(){

	if(!_isInit){
		printf( "RtspService already deinit\r\n" );
		return -1;
	}

	Medium::close(_rtspServer);
	_usage_env->reclaim();

	if( _task_scheduler ){
		delete _task_scheduler;
	}

	if( _authDB ){
		delete _authDB;
	}

	_isInit = false;

    return 0;
}

int32_t RtspService::start(){

	if( _isInit == false ){
		printf( "pls init.....\r\n" );
		return -1;
	}
	if( _isStart == true ){
		printf( "RtspService already start\r\n" );
		return -1;
	}


	_rtsp_ringBuffer = new CRingBuffer<vidPacket>(15);
	_serverMediaSession = ServerMediaSession::createNew(*_usage_env, "live", 0, "ww live test");
	_videoliveSMS = rtsp_h264liveSMS::createNew(*_usage_env, _videoSource, _rtsp_ringBuffer );
	_serverMediaSession->addSubsession(_videoliveSMS);
	_rtspServer->addServerMediaSession(_serverMediaSession);

	char * url = _rtspServer->rtspURL(_serverMediaSession);
	*_usage_env << "using url \"" << url << "\"\n";
	delete[] url;

	_usage_env->taskScheduler().doEventLoop();
	*_usage_env << "doEventLoop........\n";

	_isStart = true;

    return 0;
}



int32_t RtspService::stop(){

	if( _isInit == false ){
		printf( "pls init.....\r\n" );
		return -1;
	}
	if( _isStart == false ){
		printf( "RtspService already stop\r\n" );
		return -1;
	}

	_rtspServer->removeServerMediaSession(_serverMediaSession);
	printf("removeServerMediaSession........\n");

	Medium::close(_rtspServer);
	printf("rtspServer........\n");
	_usage_env->reclaim();

	_isStart = false;

    return 0;
}

int32_t RtspService::deliver( vidPacket *vid_pkg ){

	if( _isInit == true && _isInit == true ){
		_rtsp_ringBuffer->WriteData(vid_pkg);
	}else{
		printf( "no init or start" );
	}

	return 0;
}